import config from "@/config/config";

//小程序版本更新检测
function checkForUpdate() {
  if (uni.canIUse("getUpdateManager")) {
    // 创建 UpdateManager 实例
    const updateManager = uni.getUpdateManager();
    // 检测版本更新
    updateManager.onCheckForUpdate(function (res) {
      // 请求完新版本信息的回调
      console.log("是否有新版本：", res.hasUpdate);
      if (res.hasUpdate) {
        uni.showModal({
          title: "温馨提示",
          content: "检测到当前小程序有新版本",
        });
      }
    });
    // 监听小程序有版本更新事件
    updateManager.onUpdateReady(function () {
      // 调用 applyUpdate 应用新版本并重启
      updateManager.applyUpdate();
    });
    updateManager.onUpdateFailed(function () {
      // 新版本下载失败
      uni.showModal({
        title: "已经有新版本喽~",
        content: "请您删除当前小程序，到微信 “发现-小程序” 页，重新搜索打开哦~",
      });
    });
  } else {
    uni.showModal({
      title: "溫馨提示",
      content: "当前客户端版本过低，无法使用部分功能，请升级到最新版本后重试。",
    });
  }
}

//设置缓存
function setlocalStorage(key, val) {
  uni.setStorageSync(config.prefix + key, val);
}

//获取缓存
function getlocalStorage(key) {
  let data = uni.getStorageSync(config.prefix + key);
  return data;
}

//深拷贝
function deepCopy(data) {
  if (data && typeof data === "object") {
    //针对函数的拷贝
    if (typeof data === "function") {
      let tempFunc = data.bind(null);
      tempFunc.prototype = deepCopy(data.prototype);
      return tempFunc;
    }

    switch (Object.prototype.toString.call(data)) {
      case "[object String]":
        return data.toString();
      case "[object Number]":
        return Number(data.toString());
      case "[object Boolean]":
        return new Boolean(data.toString());
      case "[object Date]":
        return new Date(data.getTime());
      case "[object Array]":
        var arr = [];
        for (let i = 0; i < data.length; i++) {
          arr[i] = deepCopy(data[i]);
        }
        return arr;

      //js自带对象或用户自定义类实例
      case "[object Object]":
        var obj = {};
        for (let key in data) {
          //会遍历原型链上的属性方法，可以用hasOwnProperty来控制 （obj.hasOwnProperty(prop)
          obj[key] = deepCopy(data[key]);
        }
        return obj;
    }
  } else {
    return data;
  }
}

function getCustomTime(time, type = "YYYY-MM-DD hh:mm:ss") {
  let date = "";
  if (time) {
    if (typeof time == "string") {
      time = time.replace(/\-/g, "/");
      time = time.replace(/\./g, "/");
    }
    date = new Date(time);
  } else {
    date = new Date();
  }
  if (type == "timestamp") {
    return date.getTime();
  }

  let YYYY = date.getFullYear();

  //月
  let M = date.getMonth() + 1;
  let MM = M.toString().padStart(2, "0");

  //日
  let D = date.getDate();
  let DD = D.toString().padStart(2, "0");

  //时
  let h = date.getHours();
  let hh = h.toString().padStart(2, "0");

  //分
  let m = date.getMinutes();
  let mm = m.toString().padStart(2, "0");

  //秒
  let s = date.getSeconds();
  let ss = s.toString().padStart(2, "0");

  let week = date.getDay();

  type = type.replace(/YYYY/g, YYYY);

  type = type.replace(/MM/g, MM);
  type = type.replace(/M/g, M);

  type = type.replace(/DD/g, DD);
  type = type.replace(/D/g, D);

  type = type.replace(/hh/g, hh);
  type = type.replace(/h/g, h);

  type = type.replace(/mm/g, mm);
  type = type.replace(/m/g, m);

  type = type.replace(/ss/g, ss);
  type = type.replace(/s/g, s);

  type = type.replace(/week/g, week);
  return type;
}

const $common = {
  checkForUpdate,
  setlocalStorage,
  getlocalStorage,
  deepCopy,
  getCustomTime,
};

export default $common;
